package slowka;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 * Window displaying statistics after test
 * @author ssobczak
 */
public class StatsWindow extends javax.swing.JFrame {
    private final QuestionsSuite suite;

    /**
     * Creates new form StatsWindow
     * @param s Suite from finished test
     */
    public StatsWindow(QuestionsSuite s, ResourceBundle rb) {
        suite = s;
        resourceBundle = rb;
        
        initComponents();
    }

    /**
    * For testing purposes
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        HashSet<Translation> translations = new HashSet<Translation>();
        translations.add(new Translation(new Word("próba", "POLISH"), new Word("test", "ENGLISH")));

        List<Suite> suites = new LinkedList<Suite>();
        suites.add(new Suite("test", "POLISH", "ENGLISH", translations));


        final QuestionsSuite s = new QuestionsSuite(suites, QuestionAskingMode.POLISH, ResourceBundle.getBundle("slowka/ResourceBundle_pl_PL"));
        s.drawNextQuestion();
        s.checkAnswer("wrong");
        s.checkAnswer("test");
        s.checkAnswer("test");

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new StatsWindow(s, ResourceBundle.getBundle("slowka/ResourceBundle_pl_PL")).setVisible(true);
            }
        });
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {

        jToggleButton1 = new javax.swing.JToggleButton();
        jPanel1 = getChartPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jToggleButton1.setText(resourceBundle.getString("CLOSE"));
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(100)
                        .addComponent(jToggleButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30)
                .addComponent(jPanel1)
                .addGap(30)
                .addComponent(jToggleButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        dispose();
    }

    private JPanel getChartPanel() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        for(Question q : suite.getWrongAnswers()) 
            dataset.setValue(q.getMistakes(), resourceBundle.getString("MISTAKES"), q.getQuestion());
        
        JFreeChart chart = ChartFactory.createBarChart(resourceBundle.getString("NR_MISTAKES"),
                resourceBundle.getString("WORD"),
                resourceBundle.getString("MISTAKES"),
                dataset,
                PlotOrientation.HORIZONTAL, false,
                true, 
                false
         );

         ChartPanel CP = new ChartPanel(chart);
         CP.setSize(200, 30+30*suite.getWrongAnswers().size());

        return CP;
    }

    private javax.swing.JPanel jPanel1;
    private javax.swing.JToggleButton jToggleButton1;
    private ResourceBundle resourceBundle;
}
